package com.jxb.myshop.web.servlet;

import com.jxb.myshop.common.Constant;
import com.jxb.myshop.common.ResponseResult;
import com.jxb.myshop.entity.User;
import com.jxb.myshop.entity.vo.OrderVo;
import com.jxb.myshop.services.AddressServices;
import com.jxb.myshop.services.CartServices;
import com.jxb.myshop.services.ItemServices;
import com.jxb.myshop.services.OrderServices;
import com.jxb.myshop.services.impl.AddressServicesImpl;
import com.jxb.myshop.services.impl.CartServicesImpl;
import com.jxb.myshop.services.impl.ItemServicesImpl;
import com.jxb.myshop.services.impl.OrderServicesImpl;
import com.jxb.myshop.util.StringUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/orders")
public class OrdersController extends BaseController {
    /**
     * 因为这个订单要获取购物车数据以及地址数据
     * 所以给他提供这两个services
     */
    private CartServices cs = new CartServicesImpl();
    private AddressServices as = new AddressServicesImpl();
    private OrderServices os = new OrderServicesImpl();
    private ItemServices is = new ItemServicesImpl();

    /**
     * 改变订单状态
     */
    public String changeState(HttpServletRequest req, HttpServletResponse resp){
        String oid = req.getParameter("oid");
        os.changeStatus(oid);
        return Constant.Re.URL_REDIRECT + "/orders?method=getAllOrders";
    }

    /**
     * 四表联查，两两相连，address与order、item与product相连
     */
    public String getOrderDetail(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        String oid = req.getParameter("oid");
        OrderVo oa = os.getOrderAndAddress(oid);
        req.setAttribute("oa",oa);
        ResponseResult ip = is.getItemAndProduct(oid);
        req.setAttribute("ip",ip);
        return Constant.Re.URL_FORWARD + "/orderDetail.jsp";
    }

    /**
     * 生成订单详情
     */
    public String createOrder(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        int aid = StringUtil.StringToInt(req.getParameter("aid"));
        ResponseResult rr = os.createOrder(aid,user.getUid());
        return Constant.Re.URL_FORWARD + "/orders?method=getAllOrders";
    }

    /**
     * 获取所有订单
     */
    public String getAllOrders(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        if (user == null) {
            return Constant.Re.URL_FORWARD + "/login.jsp";
        }
        ResponseResult rr = os.getAllOrders(user.getUid());
        req.setAttribute("rr",rr);
        return Constant.Re.URL_FORWARD + "/orderList.jsp";
    }

    /**
     * 购物车加入订单页面
     * @param req
     * @param resp
     * @return
     */
    public String getOrderView(HttpServletRequest req, HttpServletResponse resp){
        User user = (User) req.getSession().getAttribute(Constant.SystemConstant.IS_LOGIN);
        if (user == null) {
            return Constant.Re.URL_FORWARD + "/login.jsp";
        }
        ResponseResult cartList = cs.getCartList(user.getUid());
        // 因为直接传入数据，前端就不用.data造成混乱
        req.setAttribute("cartList",cartList.getData());
        ResponseResult userAddress = as.getUserAddress(user.getUid());
        req.setAttribute("addList",userAddress.getData());
        return Constant.Re.URL_FORWARD + "/order.jsp";
    }
}
